package cn.tedu.imop.stock.controller;

import cn.tedu.imop.api.model.RestResult;
import cn.tedu.imop.api.model.RestResultUtils;
import cn.tedu.imop.common.constants.Constants;
import cn.tedu.imop.stock.entity.CouponStock;
import cn.tedu.imop.stock.service.CouponStockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@Api(tags = "库存管理")
@RestController
@RequestMapping(Constants.CONTROLLER_API_VERSION+"/stock")
public class CouponStockController {
    private final CouponStockService stockService;
    public CouponStockController(CouponStockService stockService) {
        this.stockService = stockService;
    }
    /**初始化优惠卷库存*/
    @ApiOperation("初始化库存")
    @PostMapping
    public RestResult<Void> createStock(CouponStock stock) {
        stockService.createCouponStock(stock);
        return RestResultUtils.success();
    }
    /**更新优惠卷的库存*/
    @ApiOperation("更新库存")
    @PutMapping
    public RestResult<Void> editNumByCouponId(Integer couponId) {
        stockService.editNumByCouponId(couponId);
        return RestResultUtils.success();
    }
    /**查询库存*/
    @ApiOperation("查询库存")
    @GetMapping
    public RestResult<Integer> countStock(Integer couponId) {
       CouponStock stock= stockService.getCouponStock(couponId);
        return RestResultUtils.success(stock.getNum());
    }
}
